package com.wenson.structural.flyweight;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: TileFactory
 * @Author: luowangcheng
 * @Date: 2024/6/14 8:28
 * @Description:
 */
public class TileFactory {
    private Map<String, Drawable> images;

    public TileFactory(){
        images = new HashMap<String, Drawable>();
    }

    public Drawable getDrawable(String image){
        if (!images.containsKey(image)){
            switch (image){
                case "河流":
                    images.put(image, new River());
                    break;
                case "草地":
                    images.put(image, new Grass());
                    break;
                case "道路":
                    images.put(image, new Road());
                    break;
                case "房屋":
                    images.put(image, new House());
                    break;
            }
        }
        // 至此，缓存池里必然有图件，直接取得并返回
        return images.get(image);
    }
}
